package com.paomi.client.activity.vip.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.paomi.client.R;
import com.paomi.client.activity.vip.model.CouponBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by pc on 2018/3/13.
 * 优惠券列表适配
 */

public class CouponListAdapter extends RecyclerView.Adapter<CouponListAdapter.ViewHolder> {
    Context context;
    List<CouponBean> mlist = new ArrayList<>();
    OnItemClick onClickListener;
    private Typeface mType;

    public void OnItemClick(OnItemClick onClickListener) {
        this.onClickListener = onClickListener;
    }


    public CouponListAdapter(Context context, Typeface typeface) {
        this.context = context;
        this.mType = typeface;
    }

    public void setData(List<CouponBean> list) {
        this.mlist = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_vip_coupon, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        CouponBean ticket = mlist.get(position);
        if(ticket.getImg()!=null){
            Glide.with(context).load(ticket.getImg()).into(holder.couponIv);
        }
        holder.couponName.setText(ticket.getName());
        holder.couponTxt.setText("");
        if (ticket.getIsGet() == 0) {//未领取
            holder.btn_draw.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_vip_btn));
            holder.btn_draw.setText(context.getResources().getString(R.string.my_vip_btn));
            holder.btn_draw.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onClickListener != null) {
                        onClickListener.setOnItemClick(holder.itemView, position);
                    }
                }
            });
        } else if (ticket.getIsGet() == 1) {//已领取
            holder.btn_draw.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_vip_btn));
            holder.btn_draw.getBackground().setAlpha(100);
            holder.btn_draw.setText(context.getResources().getString(R.string.my_vip_un_btn));
        }
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_draw)
        TextView btn_draw;
        @BindView(R.id.coupon_iv)
        ImageView couponIv;
        @BindView(R.id.coupon_name)
        TextView couponName;
        @BindView(R.id.coupon_txt)
        TextView couponTxt;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int pos);
    }
}
